//World.h

#include "stdafx.h"
#include "Bitmap.h"

#ifndef WORLD_H_INCLUDED
#define WORLD_H_INCLUDED

struct Player 
{
	float m_fPositionX;
	float m_fPositionY;
	float m_fRotation;
	float m_fVelocityX;	
	float m_fVelocityY;
	float m_fAccelerationX;
	float m_fAccelerationY;
	float m_fThrust;		
	float m_fSteering;	

	int m_dwScore;	

	float m_fGemPosX;		
	float m_fGemPosY;
	dword m_dwGemID;

	IPAddr m_xPlayerIp;

	int m_iLastMessageReceived;

	Player();
	~Player();
	Player(const Player& p_xS);
	Player& operator=(const Player& p_xS);
};

class World
{
public:
	World();
	~World();

	enum
	{
		Height = 100,
		Width = 100,
		Radius = 10,
		GemRadius = 5,
		InvalidGemID = -2,
		MaxPlayers = 16
	};

	struct Gem
	{
		int m_iLastSpawn;
		int m_iLastKill;
		dword m_dwGemID;
		float m_fGemPosX;
		float m_fGemPosY;

		Gem();
		~Gem();
	};

	struct Vote
	{
		dword m_xFrom;
		dword m_xTo;
		int m_iScore;
	};

	DArray<Vote> m_axVotes;
	DArray<Player*> m_axPlayers;
	DArray<Gem> m_axTakenGems;
	Gem m_xGem;

	Player* GetPlayer(IPAddr& xAddr);
	Player* GetPlayer(dword dwAddr);
	bool AddPlayer(IPAddr& xAddr);
	bool RemovePlayer(IPAddr& xAddr);

	void Update();
	void Render(Bitmap* p_pxBM);
	void BounceOffWalls(Player* p_xP);
	void CollectCollisions(DArray<POINT>& po_axPairs);
	void CollisionResponse(Player* p_xA,Player* p_xB);

	void AddVote(dword xFrom, dword xTo, int iScore);
	int FindVote(dword xFrom, dword xTo);
	void VoteScore();
	int GetMatch(Vote& xVote);
	bool IsEqualToMine(Vote xVote);

	bool IsGemTaken(dword dwId);
};

#endif
